package com.lwz.common.utils

import android.content.Context
import android.os.Parcelable
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import java.io.StringReader


/**
 * @Description: MMKV工具类
 * @Author: kim
 * @CreateDate: 2023/8/22 10:56
 * @Version: 1.0
 */
object MMKVUtil {

    var mmkv: MMKV? = null


    @Synchronized
    fun init(context: Context){
        if (mmkv == null){
            MMKV.initialize(context)
            mmkv = MMKV.defaultMMKV()
        }
    }

    /**
     * 保存 booealn
     * @param key
     * @param value
     */
    fun putBoolean(key: String?, value: Boolean) {
        mmkv?.encode(key, value)
    }

    /**
     * 获取保存的boolean
     * @param key
     * @return
     */
    fun getBoolean(key: String?): Boolean? {
        return mmkv?.decodeBool(key, false)
    }

    /**
     * 获取保存的boolean
     * @param key
     * @param defauleValue  默认值
     * @return
     */
    fun getBoolean(key: String?, defauleValue: Boolean): Boolean {
        if (mmkv == null){
            return false
        }
        return mmkv!!.decodeBool(key, defauleValue)
    }

    /**
     * 保存字符串
     * @param key
     * @param value
     */
    fun putString(key: String?, value: String?) {
        mmkv?.encode(key, value)
    }

    /**
     * 获取保存的字符串
     * @param key
     * @return
     */
    fun getString(key: String?): String? {
        return mmkv?.decodeString(key, "")
    }

    fun getString(key: String?,default:String): String? {
        return mmkv?.decodeString(key, default)
    }

    /**
     * 保存数值
     * @param key
     * @param value
     */
    fun putInt(key: String?, value: Int) {
        mmkv?.encode(key, value)
    }

    /**
     * 获取数值
     * @param key
     * @return
     */
    fun getInt(key: String?): Int{
        return mmkv!!.decodeInt(key, 0)
    }

    fun getInt(key: String?, defaultValue: Int): Int{
        return mmkv!!.decodeInt(key, defaultValue)
    }

    /**
     * long 类型
     * @param key
     * @param value
     */
    fun putLong(key: String?, value: Long) {
        mmkv?.encode(key, value)
    }

    /**
     * 获取Long
     * @param key
     * @return
     */
    fun getLong(key: String?): Long? {
        return mmkv?.decodeLong(key, 0L)
    }

    /**
     * 保存对象
     * @param key
     * @param o
     */
   inline fun <reified T> putObject(key: String?, o: T) {
        val typeS = object : TypeToken<T>(){}.type
        val json = Gson().toJson(o)
        mmkv?.encode(key, json)
    }

    /**
     * 获取对象
     * @param key
     * @param o
     * @return
     */
    inline fun <reified T> getObject(key: String?, t: T): T? {
        val typeS = object : TypeToken<T>() {}.type
        val json: String? = mmkv?.decodeString(key, "")
        val reader = StringReader(json)
        return Gson().fromJson(json, typeS)
    }

    fun <T : Parcelable> getDecodeParcelable(key: String, tClass: Class<T>?): T? {
        return mmkv!!.decodeParcelable(key, tClass)
    }


    fun <T : Parcelable> putDecodeParcelable(key: String, t: T): Boolean {
        return mmkv!!.encode(key, t)
    }


}